/*------------------------------------------------------------------------------
 * PROJ : Prototype
 * NAME : sculove.spring.framework.handler.BizSimpleMappingExceptionResolver.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.spring.framework.handler;

import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import sculove.spring.framework.exception.BizException;
import sculove.spring.framework.exception.CommitBizException;
import sculove.spring.framework.util.SStringUtil;

/**
 * <pre>
 * sculove.spring.framework.handler.BizSimpleMappingExceptionResolver.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 12. 28. 오후 6:18:31
 * @version :
 * @author : sculove
 * @deprecated
 */
public class BizSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver {

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		// Log exception, both at debug log level and at warn level, if desired.
		if (logger.isDebugEnabled()) {
			logger.debug("Resolving exception from handler [" + handler + "]: " + ex);
		}
		logException(ex, request);
		
		/*
		 * Start--. BizException 일 경우는, viewName에 명시된 view를 반환함.
		 * 단, 반환된 view값이 없을경우는 설정파일에 명시한 view를 반환함.
		 */
		String viewName = "";
		if( (ex instanceof BizException) || (ex instanceof CommitBizException) ) {
			BizException bizEx = (BizException)ex;
			if(SStringUtil.hasText(bizEx.getViewName())) {
				viewName = bizEx.getViewName();
			}
		}	
		if(!SStringUtil.hasText(viewName)) {
			// Expose ModelAndView for chosen error view.
			viewName = determineViewName(ex, request);
		}

		/*
		 * End--.
		 */
		
		if (viewName != null) {
			// Apply HTTP status code for error views, if specified.
			// Only apply it if we're processing a top-level request.
			Integer statusCode = determineStatusCode(request, viewName);
			if (statusCode != null) {
				applyStatusCodeIfPossible(request, response, statusCode.intValue());
			}
			return getModelAndView(viewName, ex, request);
		}
		else {
			return null;
		}
	}

	

}
